﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using XFControls.Utils;
using XFControls.XFPanels;
using XFControls.XFPanels.XFPanelItems;
using XFSenseUI.XFItems;

namespace XFSenseUI
{
    public partial class SenseFileBrowserPanel : XFPanelList
    {
        #region Delegates

        public delegate void FileBrowserEventHandler(object sender, TextEventArgs args);

        #endregion

        private string _curDirectory;
        private bool isTop;

        public SenseFileBrowserPanel()
        {
            _curDirectory = "\\";
            Color bc = Color.FromArgb(239, 235, 239);
            if (Parent != null)
                Parent.BackColor = bc;
            BackColor = bc;
            InitializeComponent();

            isTop = true;
        }

        public SenseFileBrowserPanel(string startFolder)
            : this()
        {
            _curDirectory = startFolder;
        }

        public string Filter { get; set; }
        public event FileBrowserEventHandler Selected;

        public SenseFileBrowserPanel SetStartPath(string path)
        {
            SenseFileBrowserPanel ret = this;

            string curPath = "\\";

            foreach (string p in path.Split('\\'))
            {
                if (!string.IsNullOrEmpty(p))
                {
                    curPath += p + "\\";

                    var right = new SenseFileBrowserPanel(curPath);
                    var back = new XFItemBackHeader();

                    right.AddHeaderItem(back);
                    ret.SetRightPanel(right);
                    ret = right;
                }
            }

            return ret;
        }

        public void Close()
        {
            clearPanels();
            _curState = States.SlideRight;
            prepSlideRight();
        }

        public override void PanelPaint(Graphics g)
        {
            if (_items.Count < 2)
            {
                fillPanel();
            }
            base.PanelPaint(g);
        }

        public new void SetRightPanel(XFPanelBase panel)
        {
            if (panel is SenseFileBrowserPanel)
                ((SenseFileBrowserPanel) panel).isTop = false;
            base.SetRightPanel(panel);
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            IXFItem itm = _selected;
            if (itm != null && _curState == States.Click)
            {
                switch (itm.GetClickResult())
                {
                    case XFItemClickResult.Confirm:
                        if (Selected != null)
                            Selected(this, new TextEventArgs(((SenseItemFileBrowserItem) itm).GetPath()));

                        while (LeftPanel is SenseFileBrowserPanel)
                        {
                            XFPanelBase lp = LeftPanel;
                            if (((SenseFileBrowserPanel) lp).Selected != null)
                                ((SenseFileBrowserPanel) lp).Selected(this,
                                                                      new TextEventArgs(
                                                                          ((SenseItemFileBrowserItem) itm).GetPath()));
                            LeftPanel = LeftPanel.LeftPanel;
                            LeftPanel.SetRightPanel(this, true);
                            lp.Dispose();
                            Application.DoEvents();
                        }

                        _curState = States.SlideRight;
                        break;
                }
            }
            base.OnMouseUp(e);
        }

        private void clearPanels()
        {
            while (LeftPanel is SenseFileBrowserPanel)
            {
                XFPanelBase lp = LeftPanel;

                LeftPanel = LeftPanel.LeftPanel;
                LeftPanel.SetRightPanel(this, true);
                lp.Dispose();
                Application.DoEvents();
            }
        }

        private void fillPanel()
        {
            if (!Directory.Exists(_curDirectory))
                _curDirectory = "\\";


            string[] dirs = Directory.GetDirectories(_curDirectory);
            string[] files = Directory.GetFiles(_curDirectory);
            var filters = new List<string>();

            if (!string.IsNullOrEmpty(Filter))
                filters.AddRange(Filter.Split(','));

            if (!isTop)
                Add(SenseItemFileBrowserItem.CreateUpOneLevel());

            foreach (string dir in dirs)
            {
                Add(new SenseItemFileBrowserItem(new DirectoryInfo(dir)));
            }

            foreach (string file in files)
            {
                string ext = file.Split('.').Last().ToLower();
                if (filters.Count == 0 || filters.Any(f => f.EndsWith(ext)))
                    Add(new SenseItemFileBrowserItem(new FileInfo(file)));
            }

            if (_items.Count < 2)
                Add("Folder is Empty");
        }

        protected override void Dispose()
        {
            base.Dispose();
        }
    }
}